package view;

import controller.AccountValidation;
import controller.OperationFile;
import controller.ToArray;
import model.FileRules;
import model.ScanRead;

import java.io.IOException;

/**
 * description: 预约界面的类
 * author: 时间
 * time: 2022-01-11-13:23
 */
public class OrderView extends ViewRun implements Rules{

    // 显示教室相关信息，需要进行过滤（数据来源于OperationFile类），
    // if flags == 3   显示普通教室
    // if flags == 4   显示智慧教室
    // if flags == 5   显示阶梯教室
    @Override
    public void views() throws IOException {
        OperationFile operationFile = new OperationFile();
        StringBuffer class1 = operationFile.readCommonClassroom();
        StringBuffer class2 = operationFile.readsmartClassroom();
        StringBuffer class3 = operationFile.readTheater();

        if (flags == 3){    // 展示普通教室
            System.out.println("************************普通教室预约****************************");
            view1(class1);
        }else if (flags == 4){    // 展示智慧教室
            System.out.println("************************智慧教室预约****************************");
            view1(class2);
        }else if (flags == 5){     // 展示阶梯教室
            System.out.println("************************阶梯教室预约****************************");
            view1(class3);
        }
        flag1 = true;
        while (flag1){
            System.out.print("请输入教室号(-1退出)：");
            String number = ScanRead.readClassroom();
            if (number.equals("-1")){
                break;
            }
            if (!AccountValidation.classNumExist(number, pattern1)){
                System.out.print("输入教室号不存在，请重新输入");
                continue;
            }
            if (!AccountValidation.freeExist(number, pattern2, operationFile.readClassroomFile())){
                System.out.println("当前教室状态不能预约(非空闲)");
                continue;
            }

            System.out.print("请输入预约日期：");
            String date = ScanRead.readOrderDate();
            System.out.print("请输入预约时间(10：00-12：00)：");
            String time = ScanRead.readOrderTime();
            System.out.print("请输入手机号：");
            String phone = ScanRead.readPhone();
            System.out.print("请输入备注：");
            String to = ScanRead.readClassroom();
            if (flags == 4){
                operationFile.write(LoginView.account + "-" + number + "-" + Rules.CLASS1 + "-"
                + time + "-" + phone + "-" + to + "-" + date);
                operationFile.updateOrderClassroom(number, number + "-" + CLASS1 + "-" + COUNT1
                        + "-" + STATE3 + "-" + to);
            }else if (flags == 3){
                operationFile.write(LoginView.account + "-" + number + "-" + Rules.CLASS2 + "-"
                        + time + "-" + phone + "-" + to +"-" + date);
                operationFile.updateOrderClassroom(number, number + "-" + CLASS2 + "-" + COUNT2
                        + "-" + STATE3 + "-" + to);
            }else if (flags == 5){
                operationFile.write(LoginView.account + "-" + number + "-" + Rules.CLASS3 + "-"
                        + time + "-" + phone + "-" + to + "-" + date);
                operationFile.updateOrderClassroom(number, number + "-" + CLASS3 + "-" + COUNT3
                        + "-" + STATE3 + "-" + to);
            }
            System.out.println("预约成功");
            ScanRead.readReturn();
            break;
        }
        exit();
    }

    private void view1(StringBuffer class1){
        System.out.println("教室号\t\t教室类型\t\t可容纳人数\t\t教室状态\t\t备注");
        ToArray toArray = new ToArray();
        String[][] strings = toArray.fileToArray(class1, FileRules.pathCl, Rules.pattern2);
        //查询预约信息
        for (int i = 0; i < strings.length; i++) {
            if (strings[i] != null){
                System.out.println(strings[i][0] + "\t\t" + strings[i][1] + "\t\t" + strings[i][2]
                        + "\t\t\t" + strings[i][3] + "\t\t" + strings[i][4]);
            }
        }
        System.out.println("***************************************************************");
    }

    // 将flags置1
    @Override
    public void exit() {
        flags = 1;
    }
}
